<template>
  <div class="hyydxq">
    <van-tabs
      color="#37B5B5"
      title-inactive-color="#999999"
      title-active-color="#333333"
      v-model="active"
      @click="onClick"
    >
      <van-tab title="通知消息"> </van-tab>
      <van-tab title="咨询消息"> </van-tab>
    </van-tabs>

    <van-pull-refresh style="margin-top: 15px" v-model="refreshing" @refresh="onRefresh">
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
        <div v-if="active == 1">
          <div v-if="list.length > 0">
            <div v-for="(item, key) in list" :key="key">
              <p class="titleStyle">{{ item.createTime }}</p>
              <van-cell class="approvalItem content" @click="problemContent(item)">
                <div class="flex-between font-13 line-height-3">
                  <div class="col-666666 m-r-10">
                    <p class="col-333333 font-bold-500 font-15">
                      <span>{{ item.messageContent }}</span> <i v-if="item.readStatus == 0"></i>
                    </p>
                    <p>
                      {{
                        item.type == 0
                          ? (!!item.questionInfoModel &&
                              !!item.questionInfoModel.userInfo &&
                              (item.questionInfoModel.userInfo.mobilePhone + '').replace(
                                !!item.questionInfoModel &&
                                  !!item.questionInfoModel.userInfo &&
                                  (item.questionInfoModel.userInfo.mobilePhone + '').substring(3, 7),
                                '****'
                              )) ||
                            ''
                          : item.updateTime
                      }}
                    </p>
                    <p class="twoOut">
                      {{ item.questionInfoModel.content }}
                    </p>
                  </div>
                  <img width="12px" height="12px" src="../../assets/img/rightJt.png" />
                </div>
              </van-cell>
            </div>
          </div>
          <temporarily-data v-else />
        </div>

        <div v-if="active == 0">
          <div v-if="NoticeList.length > 0">
            <div v-for="(item, key) in NoticeList" :key="key">
              <p class="titleStyle">{{ item.createTime }}</p>
              <van-cell class="approvalItem content" @click="noticeDetail(item)">
                <div class="flex-between font-13 line-height-2">
                  <div class="col-666666 m-r-10" style="width:100%;">
                    <p class="col-333333 font-bold-500 font-15 haveRead" style="width:100%;">
                      <span>{{ item.title }}</span><i v-if="item.readStatus == 0"></i>
                    </p>

                    <p class="twoOut">
                      {{ item.content }}
                    </p>
                    <div class="btmDetails">
                      <span>查看详情</span>
                      <img width="12px" height="12px" src="../../assets/img/rightJt.png" />
                    </div>
                  </div>
                  
                </div>
              </van-cell>
            </div>
          </div>
          <temporarily-data v-else />
        </div>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
//保养记录
import { getValue, setValue } from '@/utils/auth'
import {
  Toast,
  Cell,
  CellGroup,
  Popup,
  Picker,
  DatetimePicker,
  PullRefresh,
  List,
  Search,
  RadioGroup,
  Radio,
  Tab,
  Tabs
} from 'vant'
import { getHandle, postHandle } from '@/api'
import dayjs from 'dayjs'
import TemporarilyData from '@/components/temporarilyData'
export default {
  components: {
    TemporarilyData,
    [Cell.name]: Cell,
    [CellGroup.name]: CellGroup,
    [Popup.name]: Popup,
    [Picker.name]: Picker,
    [DatetimePicker.name]: DatetimePicker,
    [PullRefresh.name]: PullRefresh,
    [Toast.name]: Toast,
    [List.name]: List,
    [Search.name]: Search,
    [RadioGroup.name]: RadioGroup,
    [Radio.name]: Radio,
    [Tab.name]: Tab,
    [Tabs.name]: Tabs
  },
  data() {
    return {
      active: 0,
      keyword: '',
      refreshing: false,
      currentDate: new Date(),
      list: [],
      NoticeList: [],
      loading: false,
      finished: false,
      params: {
        page: 1,
        perPage: 10
      },
      swFlag: getValue('swFlag') || '',
      productId: getValue('productId') || '',
      role_type: getValue('role_type') || 0,
      loginUserId: getValue('loginUserId')
    }
  },
  watch: {},
  created() {},
  mounted() {
    this.$bridge.register('refresh', data => {
      this.refreshing = true
      this.onRefresh()
    })
  },
  methods: {
    onClick(e) {
      this.refreshing=TextTrackCue
      console.log(e)
      this.active = e
      this.onRefresh()
    },
    problemContent(item) {
      setValue('hfxxid', item.questionInfoModel.id)
      if (item.readStatus == 0) {
        this.setupRead(item)
      } else {
        if (item.type == 0) {
          this.whetherReply(item.questionInfoModel.id)
        } else {
          this.$bridge.call('replyDetails')
        }
      }
    },
    setupRead(item) {
      postHandle(`/agri/api/common/advisoryMessage/updateReadStatus/${item.id}`).then(res => {
        if (res.status == 0) {
          if (item.type == 0) {
            this.whetherReply(item.questionInfoModel.id)
          } else {
            this.$bridge.call('replyDetails')
          }
        }
      })
    },
    whetherReply(id) {
      getHandle(`/agri/api/common/questionInfo/queryDetail/${id}?expertUserId=${this.loginUserId}`).then(res => {
        if (res.status === 0) {
          const whyReplyStatus =
            res.data.questionReplyInfoModels.length > 0 ? res.data.questionReplyInfoModels[0].replyStatus : ''
          if (whyReplyStatus === 0 && whyReplyStatus !== '') {
            this.$bridge.call('detailsReplied')
          } else if (whyReplyStatus === 1 && whyReplyStatus !== '') {
            this.$bridge.call('expertReplied')
          } else {
            if (res.data.replyStatus === 0) {
              this.$bridge.call('detailsReplied')
            } else if (res.data.replyStatus === 1) {
              this.$bridge.call('expertReplied')
            } else {
              Toast('whyReplyStatus:' + whyReplyStatus)
            }
          }
        }
      })
    },
    // 卡片列表
    onRefresh() {
      this.params.page = 1
      this.finished = false

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true
      this.onLoad()
    },
    onSearch() {
      this.params.page = 1
      this.list = []
      this.NoticeList=[]
      this.finished = false

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true
      this.onLoad()
    },
    onLoad() {
      if (this.active === 0) {
        this.params.enterpriseInfoSubjectInfoId = getValue('subjectInfoId')
        getHandle('/agri/api/yzbzh/noticeInfo/queryNoticeListPage', this.params).then(res => {
          if (this.refreshing) {
            this.NoticeList = []
            this.refreshing = false
          }

          this.NoticeList = this.NoticeList.concat(res.data.rows)
          this.loading = false
          if (this.NoticeList.length >= res.data.count) {
            this.finished = true
          } else {
            this.params.page = this.params.page + 1
          }
        })
      } else {
        getHandle('/agri/api/common/advisoryMessage/queryListPage', this.params).then(res => {
          if (this.refreshing) {
            this.list = []
            this.refreshing = false
          }

          this.list = this.list.concat(res.data.rows)
          this.loading = false
          if (this.list.length >= res.data.count) {
            this.finished = true
          } else {
            this.params.page = this.params.page + 1
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.haveRead{
  display: flex;
  align-items: center;
  justify-content: space-between;
  >i{
    position: absolute;
    right: 10px;
  }
}
.btmDetails{
  display: flex;
  align-items: center;
  width: 100%;
  justify-content: space-between;
  margin-top: 10px;
}
.twoOut {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  line-height: 20px;
}
.titleStyle {
  text-align: center;
  padding: 16px 0 6px 0;
  color: #999999;
  font-size: 13px;
}
.topCeiling {
  position: fixed;
  top: 0;
  z-index: 1;
}
.van-field__control {
  border: 0 !important;
}
.content {
  //   border-bottom: 1px solid #eee;
  padding: 10px 15px;
}
::v-deep .van-search {
  width: 98%;
  margin: auto;
}
::v-deep .van-search__content {
  background-color: #fff;
  border-radius: 34px;
}
.flex-between {
  align-items: center;
}
.col-333333 {
  display: flex;
  align-items: center;
  > i {
    width: 8px;
    height: 8px;
    display: inline-block;
    background-color: #ff806c;
    border-radius: 4px;
    margin-left: 7px;
  }
}
.page_title {
  width: 100%;
  height: 44px;
  background-color: #37b5b5;
  padding: 0 10px;
  box-sizing: border-box;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: fixed;
  top: 0;
  z-index: 10;
  span {
    font-size: 18px;
    color: #fff;
  }
  img {
    width: 20px;
    height: 20px;
  }
  > P {
    width: 20px;
    height: 1px;
  }
}
</style>
